<%@ page import="java.text.NumberFormat" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.nus.beans.User" %>
<%@ page import="java.util.List" %>
<%@ page import="com.nus.beans.UserType" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="org.apache.commons.lang3.StringEscapeUtils" %>
<%@ page import="com.nus.beans.UserSubType" %>
<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 9/21/12
  Time: 12:10 AM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Update User</title></head>
<%
    User targetUser = (User) request.getAttribute("targetUser");
    User user = (User) session.getAttribute("user");
    boolean isNotHRManager = !user.getUserType().equals(UserType.HR_MGR);
    String errorMessage = (String) request.getAttribute("ERROR_MESSAGE");

%>
<body>
<table>
    <form method="post" action="updateUser.do">
        <tr>
            <td>Login Name:</td>
            <td><input type="text" name="loginName" value="<%= StringEscapeUtils.escapeXml(targetUser.getLoginName()) %>" readonly="true"/></td>
        </tr>
        <tr>
            <td>User Type:</td>

            <%if(isNotHRManager) { %>
            <td>
            <input type="text" name="userType" readonly="true" value="<%=targetUser.getUserType()%>"/>
                </td>
            <% } else { %>
            <td><select name="userType">
                <%
                    UserType[] userTypes = UserType.values();
                    for (UserType userType : userTypes) {
                %>
                <% if (targetUser.getUserType().equals(userType)) { %>
                <option value="<%=userType.toString()%>" selected="true"><%=userType.toString()%>
                        <% } else { %>
                <option value="<%=userType.toString()%>"><%=userType.toString()%>
                    <% } %>
                </option>
                <%}%>
            </select>
            </td>
            <% } %>
        </tr>
        <tr>
        <td>Developer Sub Type:</td>
            <%if(isNotHRManager) { %>
        <td>
            <input type="text" name="userSubType" readonly="true" value="<%=targetUser.getUserSubType()%>"/>
                </td>


            <% } else { %>

        <td> <select name="userSubType">
            <%UserSubType[] userSubTypes = UserSubType.values();
                    for (UserSubType userSubType : userSubTypes) {%>
                     <% if (targetUser.getUserSubType().equals(userSubType)) { %>
                <option value="<%=userSubType.toString()%>" selected="true"><%=userSubType.toString()%>
                        <% } else { %>
                <option value="<%=userSubType.toString()%>"><%=userSubType.toString()%>
                    <% } %>
                    <%}%>
                </select>
        </td>
            <% } %>
    </tr>

        <tr>
            <td>First Name:</td>
            <td><input type="text" name="firstName" value="<%= StringEscapeUtils.escapeXml(targetUser.getFirstName()) %>" maxlength="255"
                    <%if(isNotHRManager){%>
                       readonly="true"
                        <% } %>
                    /></td>
        </tr>
        <tr>
            <td>Family Name:</td>
            <td><input type="text" name="familyName" value="<%= StringEscapeUtils.escapeXml(targetUser.getLastName()) %>" maxlength="255"
                       <%if(isNotHRManager){%>
                       readonly="true"
                        <% } %>/></td>
        </tr>
        <tr>
            <td>Date Of Birth:</td>

            <%

                Date date = targetUser.getDateOfBirth();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);

                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH) + 1;
                int dateofMonth = calendar.get(Calendar.DATE);


            %>
            <td><select name="yearFrom">
                <%
                    for (int i = 1950; i < 2100; i++) {
                        if (i == year) {
                %>
                <option value="<%=i%>" selected="true">
                        <% } else { %>

                <option value="<%=i%>">
                    <% } %>
                    <%=i%>
                </option>
                <%}%>
            </select>
                <select name="monthFrom">
                    <%
                        for (int j = 1; j < 13; j++) {
                            if (j == month) {
                    %>
                    <option value="<%=j%>" selected="true">
                            <% } else { %>
                    <option value="<%=j%>">
                        <% } %>
                        <%=j%>
                    </option>
                    <%}%>
                </select>
                <select name="dayFrom">
                    <%
                        for (int k = 1; k < 32; k++) {
                            if (k == dateofMonth) {
                    %>
                    <option value="<%=k%>" selected="true">
                            <% } else { %>
                    <option value="<%=k%>">
                        <% } %>
                        <%=k%>
                    </option>
                    <%}%>
                </select>
            </td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><input type="text" name="address1" value="<%= StringEscapeUtils.escapeXml(targetUser.getAddress()) %>" maxlength="255"/></td>
        </tr>
        <tr>
            <td>Phone Number:</td>
            <td><input type="text" name="phoneNo" value="<%= targetUser.getPhoneNumber() %>" maxlength="10"/></td>
        </tr>

        <tr>
            <td>Basic Salary</td>
            <td><input type="text" name="Salary" value="<%= targetUser.getBasicSalary() %>"
                   <%if(isNotHRManager){%>
                       readonly="true"
                        <% } %>
                       maxlength="8"/></td>
        </tr>
        <tr>
            <td>Leave Available</td>
            <td><input type="text" name="leaves" value="<%= targetUser.getRemainingLeaves() %>" maxlength="2"
                       <%if(isNotHRManager){%>
                       readonly="true"
                        <% } %>/></td>
        </tr>
        <tr>
            <% if (errorMessage != null){ %>
            <td colspan="2"><%=errorMessage%>
            </td>
            <% } %>
        </tr>
        <tr>
            <td><input type="submit" value="Update"/></td>
            <td><a href="viewHome.do">Home</a></td>
        </tr>
    </form>
</table>

</body>
</html>